package com.lxp.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lxp.service.VodService;
import com.lxp.utils.ConstantVodUtils;
import com.lxp.utils.InitVodCilent;
import com.lxp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/vod")
public class VodController {

    @Autowired
    private VodService service;
    //上传视频
    @PostMapping("/uploadVod")
    public R uploadVod(MultipartFile file){
        String vodId=service.uploadVod(file);
        return R.ok().data("vodId",vodId);
    }

    //删除视频
    @DeleteMapping("/deleteVod/{vodId}")
    public R deleteVod(@PathVariable String vodId){

        service.deleteVod(vodId);
        return R.ok();
    }
    //根据视频id,获取视频播放凭证
    @GetMapping("/getPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {

        //获取阿里云存储相关常量
        String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = InitVodCilent.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
